package org.hxc.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;


/**
 *
 * @author hxc
 * @date 2024/10/28 14:53
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("sysUserService")
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder(){
        // 密码加密
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * permitAll()  表示所匹配的URL任何人都允许访问
     * anonymous()  表示可以匿名访问匹配的URL。和permitAll()效果类似,只是设置为anonymous()的url会执行filterChain中的filter
     * denyAll()    表示所匹配的URL都不允许被访问。
     * authenticated()  表示所匹配的URL都需要被认证才能访问
     * rememberMe() 允许通过remember-me登录的用户访问
     * access() SpringEl表达式结果为true时可以访问
     * fullyAuthenticated() 用户完全认证可以访问(非remember-me下自动登录)
     * hasRole()    如果有参数,参数表示角色,则其角色可以访问
     * hasAnyRole()     如果有参数表示角色,则其中任何一个角色可以访问
     * hasAuthority()   如果有参数表示权限,则其权限可以访詞
     * hasAnyAuthority()    如果有参数表示权限,则其中任何一个权限可以访问
     * haslpAddress()   如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
     * @param http the {@link HttpSecurity} to modify
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                    .antMatchers("/api/**").permitAll() //允许匿名访问
                    .anyRequest().authenticated() //其他地址需要认证
                    .and()
                .formLogin()    //启用表单登录
//                    .loginPage("/login") //自定义登录页面
                    .permitAll()
                    .and()
                .logout() //启用退出登录
                    .permitAll();
    }

    /**
     * Configure the authentication manager builder.
     * @param auth the {@link AuthenticationManagerBuilder} to use
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder());
    }
}
